package com.jingfu.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jingfu.util.SpringUtils;
import org.apache.shiro.cache.MapCache;
import org.apache.shiro.util.SoftHashMap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zalando.problem.jackson.ProblemModule;
import org.zalando.problem.violations.ConstraintViolationProblemModule;

import java.text.SimpleDateFormat;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/1/17 下午4:54
 **/
@Configuration
public class AppConfig {

    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper mapper = new ObjectMapper().registerModules(
                new ProblemModule(),
                new ConstraintViolationProblemModule());
        //设置日期格式
        SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(smt);
        return mapper;
    }

    @Bean
    public SpringUtils springUtils(){
        return new SpringUtils();
    }

    /**
     * SoftHashMap()是shiro提供的利用SoftReference和ConcurrentHashMap实现的本地缓存，可以安全的在本地内存使用缓存
     * @return
     */
    @Bean("memoryCache")
    public MapCache<String, Object> memoryCache() {
        MapCache<String, Object> memoryCache = new MapCache<>("memoryCache", new SoftHashMap<>());
        return memoryCache;
    }
}
